package IRW

type byteWriter struct {
	buf    []byte
	offset int
}

func NewByteWriter() *byteWriter {
	return &byteWriter{}
}

func (e *byteWriter) Write(p []byte) (n int, err error) {
	// 检查是否需要扩容
	needed := e.offset + len(p)
	if needed > cap(e.buf) {
		newBuf := make([]byte, needed, 2*needed)
		copy(newBuf, e.buf)
		e.buf = newBuf
	} else if needed > len(e.buf) {
		e.buf = e.buf[:needed]
	}

	copy(e.buf[e.offset:], p)
	e.offset += len(p)
	return len(p), nil
}

func (e *byteWriter) Bytes() []byte {
	return e.buf[:e.offset]
}

func (e *byteWriter) Reset() {
	e.offset = 0
}
